﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Dot2Silverlight.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ServiceTranslator
    {
        [OperationContract]
        public object WriteDotFile(string dot)
        {
            GeneralFunctions general = new GeneralFunctions();
            string path = System.Web.HttpContext.Current.Server.MapPath("ClientBin");
            string target_path = System.Web.HttpContext.Current.Server.MapPath("ClientBin");
            String session_id = (string)System.Web.HttpContext.Current.Session.SessionID;
            session_id = session_id.Length > 4 ? session_id.Substring(0, 4) : session_id;
            string source_file = path + "\\final_" + session_id + ".dot";
            string target_file = target_path + "\\final_" + session_id + ".xdot";

            StreamWriter sw = new StreamWriter(source_file);
            sw.Write(dot);
            sw.Close();

            string error = general.ExecuteCommandDot(source_file, target_file);

            if (!error.Equals(""))
            {
                return error;
            }
            return "final_" + session_id + ".xdot";
        }

        // Add more operations here and mark them with [OperationContract]
    }
}
